package net.adst.ui.other;

import net.adst.resources.Constants;
import net.adst.util.GBC;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 22.12.12
 * Time: 2:17
 * To change this template use File | Settings | File Templates.
 */
public class AddItemsDialog extends JDialog {
    private JRadioButton outRadioButton;
    private JRadioButton naemRadioButton;
    private boolean isNaem;
    private AddOurPanel ourPanel;
    private AddNaemPanel naemPanel;


    public AddItemsDialog(int index) {
        if (index == 0) {
            isNaem = true;
        } else {
            isNaem = false;
        }
        setLayout(new GridBagLayout());
        addElements();
        addLogic();
    }

    public void addElements() {
        naemRadioButton = new JRadioButton("Добавить в наем. технику", isNaem);
        outRadioButton = new JRadioButton("Добавить в нашу технику", !isNaem);
        naemRadioButton.setBackground(Constants.COLOR_LIGHT_BLUE);
        outRadioButton.setBackground(Constants.COLOR_LIGHT_BLUE);
        naemRadioButton.setFocusPainted(false);
        outRadioButton.setFocusPainted(false);
        final ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(naemRadioButton);
        buttonGroup.add(outRadioButton);


        naemPanel = new AddNaemPanel();
        ourPanel = new AddOurPanel();
        JPanel radioPanel = new JPanel();
        radioPanel.setBackground(Constants.COLOR_LIGHT_BLUE);
        radioPanel.setLayout(new GridBagLayout());
        radioPanel.add(naemRadioButton, new GBC(0, 0).setAnchor(GBC.WEST));
        radioPanel.add(outRadioButton, new GBC(1, 0).setAnchor(GBC.WEST));
        JPanel voidPanel = new JPanel();
        voidPanel.setMaximumSize(new Dimension(0, getSize().height));
        voidPanel.setPreferredSize(new Dimension(0, getSize().height));
        voidPanel.setMinimumSize(new Dimension(0, getSize().height));
        voidPanel.setBackground(Constants.COLOR_LIGHT_BLUE);
        add(radioPanel, new GBC(0, 0).setAnchor(GBC.NORTHWEST).setInsets(10));
        add(voidPanel, new GBC(1, 0).setAnchor(GBC.NORTH).setWeight(1, 0).setFill(GBC.HORIZONTAL));
        add(ourPanel, new GBC(0, 1, 2, 1).setFill(GBC.BOTH).setWeight(1, 1));
        add(naemPanel, new GBC(0, 1, 2, 1).setFill(GBC.BOTH).setWeight(1, 1));
        naemPanel.setVisible(isNaem);
        ourPanel.setVisible(!isNaem);
    }

    public void init() {
        setResizable(true);
        setDialogSettings();
        getContentPane().setBackground(Constants.COLOR_LIGHT_BLUE);
        setSize(955, 650);
        setVisible(true);

    }

    private void setDialogSettings() {

        setModal(true);
        setModalityType(ModalityType.APPLICATION_MODAL);
        pack();
        setLocationRelativeTo(Frame.getFrames()[0]);
        setTitle("Добавление записи в таблицу");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    }

    private void addLogic() {
        ItemListener listener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.DESELECTED) return;
                if (naemRadioButton.isSelected()) {
                    isNaem = true;
                    naemPanel.setVisible(true);
                    ourPanel.setVisible(false);
                } else if (outRadioButton.isSelected()) {
                    isNaem = false;
                    naemPanel.setVisible(false);
                    ourPanel.setVisible(true);
                }

            }
        };
        naemRadioButton.addItemListener(listener);
        outRadioButton.addItemListener(listener);

    }
}
